home *** CD-ROM | disk | FTP | other *** search
- /* Graphic Load Average
- © 1987 Peter Korn
-
- This is a little program designed
- to run in the backround under MultiFinder.
- It's purpose: to graphically indicate
- the 'load' by creating a column chart of
- the load.
-
- */
- /* Defines */
-
- #define NIL 0
- #define nullStopMask 0
- #define MYALERT 128
- #define MYDIALOG 150
- #define MYWINDOW 128
- #define DESK_ID 128
- #define FILE_ID 129
- #define EDIT_ID 130
- #define CONFIGURE_ID 131
- #define BASE_ID 200 /* heirarchical menus */
- #define WINDOW_POSh 150
- #define WINDOW_POSv 151
- #define BAROFFSET 10
- #define WNETrapNum 0x60
- #define UnImplTrapNum 0x9F
- #define SUSPEND_RESUME_EVT 15
- #define SUSPEND_RESUME_BIT 0
- #define CLIPBOARD_BIT 0
- #define MessageDialog 258
-
- /* Includes */
-
- #include <Quickdraw.h>
- #include <MacTypes.h>
- #include <FontMgr.h>
- #include <WindowMgr.h>
- #include <MenuMgr.h>
- #include <TextEdit.h>
- #include <DialogMgr.h>
- #include <EventMgr.h>
- #include <DeskMgr.h>
- #include <FileMgr.h>
- #include <ToolboxUtil.h>
- #include <ControlMgr.h>
- #include <OSUtil.h>
- #include <ResourceMgr.h>
- #include <modified_MultiFinder.h>
-
- /* Globals */
-
- typedef struct ConfigurationItem {
- int value;
- Handle valueHandle;
- MenuHandle menuHandle;
- } ConfigurationItem;
-
- typedef struct ConfigurationTriplet {
- ConfigurationItem ceiling;
- ConfigurationItem interval;
- ConfigurationItem width;
- };
-
- struct ConfigurationTriplet theConfiguration[4];
-
- long ticksGoneBy, tickSum[4], tickSumDivider[4];
- int **windowPoshHandle, **windowPosvHandle;
- GrafPtr theCurrentPort;
- MenuHandle deskMenu, fileMenu, editMenu, configureMenu;
- WindowPtr loadWindow;
- Str255 daName;
- Rect dragBoundsRect;
-
- /* routines... */
-
- /* restartProc */
- restartProc()
- {
- quitTime();
- }
-
- /* the Switches */
-
- long CeilingSwitch (itemNumber)
- int itemNumber;
- {
- long returnVal;
-
- switch(itemNumber){
- case 1:
- returnVal = 1;
- break;
- case 2:
- returnVal = 2;
- break;
- case 3:
- returnVal = 3;
- break;
- case 4:
- returnVal = 4;
- break;
- case 5:
- returnVal = 5;
- break;
- case 6:
- returnVal = 6;
- break;
- case 7:
- returnVal = 7;
- break;
- case 8:
- returnVal = 8;
- break;
- case 9:
- returnVal = 9;
- break;
- case 10:
- returnVal = 10;
- break;
- case 11:
- returnVal = 15;
- break;
- case 12:
- returnVal = 20;
- break;
- case 13:
- returnVal = 30;
- break;
- case 14:
- returnVal = 60;
- break;
- }
-
- return(returnVal);
- }
-
-
- long IntervalSwitch(itemNumber)
- int itemNumber;
- {
- long returnVal;
-
- switch(itemNumber){
- case 1:
- returnVal = 1;
- break;
- case 2:
- returnVal = 2;
- break;
- case 3:
- returnVal = 5;
- break;
- case 4:
- returnVal = 10;
- break;
- case 5:
- returnVal = 20;
- break;
- case 6:
- returnVal = 60;
- break;
- case 7:
- returnVal = 120;
- break;
- case 8:
- returnVal = 180;
- break;
- case 9:
- returnVal = 600;
- break;
- case 10:
- returnVal = 1200;
- break;
- }
-
- return(returnVal);
- }
-
- long WidthSwitch(itemNumber)
- int itemNumber;
- {
- long returnVal;
-
- switch(itemNumber){
- case 1:
- returnVal = 1;
- break;
- case 2:
- returnVal = 2;
- break;
- case 3:
- returnVal = 3;
- break;
- case 4:
- returnVal = 4;
- break;
- case 5:
- returnVal = 5;
- break;
- }
- return(returnVal);
- }
-
-
- /* doMessage */
-
- doMessage (message0,message1,message2,message3)
- Str255 message0;
- Str255 message1;
- Str255 message2;
- Str255 message3;
- {
- DialogPtr dialogP;
- int item;
-
- ParamText(message0, message1, message2, message3);
- dialogP = GetNewDialog(MessageDialog, (Ptr)NIL, (Ptr)-1);
- if (dialogP == NIL) {
- SysBeep(5);
- ExitToShell();
- }
- else {
- InitCursor();
- ModalDialog((Ptr)NIL, item);
- DisposDialog(dialogP);
- }
-
- }
-
- /* setUpWorld */
-
- setUpWorld()
- {
- int i;
- SysEnvRec theWorld;
- OSErr err;
-
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent,nullStopMask);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(restartProc);
- InitCursor();
-
- /* check if WNE is implemented, and exit if not
- ...stolen from Tech Note #158 */
- err = SysEnvirons(1, &theWorld);
- if ((theWorld.machineType < 0) ||
- (NGetTrapAddress(WNETrapNum,ToolTrap) == NGetTrapAddress(UnImplTrapNum,ToolTrap))) {
- doMessage("\pMultifinder not active","","","");
- ExitToShell();
- }
- SetRect(&dragBoundsRect,
- screenBits.bounds.left +4,
- screenBits.bounds.top + MBarHeight, /* menubar height might change... */
- screenBits.bounds.right -4,
- screenBits.bounds.bottom -4);
-
-
- /* get saved resources & don't let 'em get purged */
-
- OpenResFile("\pGLA appl"); /* note this is not nice! */
-
- for (i = 0; i < 4; i++) {
- theConfiguration[i].ceiling.valueHandle = GetResource((ResType) 'INFO', (BASE_ID + i*3));
- HNoPurge(theConfiguration[i].ceiling.menuHandle);
- theConfiguration[i].ceiling.value = CeilingSwitch(**theConfiguration[i].ceiling.valueHandle);
-
- theConfiguration[i].interval.valueHandle = GetResource((ResType) 'INFO', (BASE_ID + 1 + i*3));
- HNoPurge(theConfiguration[i].interval.menuHandle);
- theConfiguration[i].interval.value = IntervalSwitch(**theConfiguration[i].interval.valueHandle);
-
- theConfiguration[i].width.valueHandle = GetResource((ResType) 'INFO', (BASE_ID + 2 + i*3));
- HNoPurge(theConfiguration[i].width.menuHandle);
- theConfiguration[i].width.value = WidthSwitch(**theConfiguration[i].width.valueHandle);
- }
-
- /* call ResError() after these, and set them manually if get error */
- windowPoshHandle = (int **) GetResource( (ResType) 'INFO', WINDOW_POSh);
- HNoPurge(windowPoshHandle);
-
- windowPosvHandle = (int **) GetResource( (ResType) 'INFO', WINDOW_POSv);
- HNoPurge(windowPosvHandle);
-
- /* if point not inside dragBoundsRect, set it to default */
- if ((**windowPoshHandle < dragBoundsRect.left) ||
- (**windowPoshHandle > dragBoundsRect.right) ||
- (**windowPosvHandle > dragBoundsRect.bottom) ||
- (**windowPosvHandle < dragBoundsRect.top)) {
-
- **windowPoshHandle = (int) ((dragBoundsRect.right - dragBoundsRect.left)/2);
- **windowPosvHandle = (int) ((dragBoundsRect.bottom - dragBoundsRect.top)/2);
- }
-
- loadWindow = GetNewWindow(MYWINDOW, (Ptr) NIL, (Ptr) -1);
- MoveWindow(loadWindow, **windowPoshHandle, **windowPosvHandle, FALSE);
- ShowWindow(loadWindow);
-
- SetPort(loadWindow);
- PenNormal();
-
- }
-
-
- /* setUpMenus */
-
- setUpMenus()
- {
- int i;
-
- deskMenu = GetMenu(DESK_ID);
- AddResMenu(deskMenu, 'DRVR');
- InsertMenu(deskMenu, 0);
-
- fileMenu = GetMenu(FILE_ID);
- InsertMenu(fileMenu, 0);
-
- editMenu = GetMenu(EDIT_ID);
- InsertMenu(editMenu, 0);
-
- configureMenu = GetMenu(CONFIGURE_ID);
- InsertMenu(configureMenu, 0);
-
- for (i = 0; i < 4; i++) {
-
- theConfiguration[i].ceiling.menuHandle = GetMenu(BASE_ID + i*3);
- InsertMenu(theConfiguration[i].ceiling.menuHandle, -1);
- CheckItem(theConfiguration[i].ceiling.menuHandle, (int) **theConfiguration[i].ceiling.valueHandle, TRUE);
-
- theConfiguration[i].interval.menuHandle = GetMenu(BASE_ID + i*3 + 1);
- InsertMenu(theConfiguration[i].interval.menuHandle, -1);
- CheckItem(theConfiguration[i].interval.menuHandle, (int) **theConfiguration[i].interval.valueHandle, TRUE);
-
- theConfiguration[i].width.menuHandle = GetMenu(BASE_ID + i*3 + 2);
- InsertMenu(theConfiguration[i].width.menuHandle, -1);
- CheckItem(theConfiguration[i].width.menuHandle, (int) **theConfiguration[i].width.valueHandle, TRUE);
- }
-
- DrawMenuBar();
- }
-
-
-
- /* doMenu */
-
- doMenu(menuResult)
- long menuResult;
- {
- int menuID, itemNumber, newHPos, newVPos, itemHit, i, configSetting;
- long longPointer;
- ProcPtr myFilterProc;
-
- /* if (menuResult == (long) NIL)
- menuResult = MenuChoice();
- */
- menuID = HiWord(menuResult);
- itemNumber = LoWord(menuResult);
-
- switch(menuID)
- {
- case DESK_ID: /* Apple Menu */
- if (itemNumber == 1){
- Alert(MYALERT, NIL);
- break;
- }
- else if (itemNumber == 0) /* b/c of heirarchical menus... */
- break;
- else
- {
- GetItem(deskMenu, itemNumber, &daName);
- GetPort(&theCurrentPort);
- OpenDeskAcc(&daName);
- SetPort(theCurrentPort);
-
- break;
- }
- break;
-
- case FILE_ID: /* the File Menu */
- switch(itemNumber)
- {
- case 1: /* the print command */
- break;
- case 2: /* the quit command */
- quitTime();
- break;
- }
- break;
-
- case EDIT_ID: /* the Edit Menu */
- switch(itemNumber){
- case 1: /* the undo command */
- break;
- case 2: /* just dashed lines */
- break;
- case 3: /* the cut command */
- break;
- case 4: /* the copy command */
- break;
- case 5: /* the paste command */
- break;
- case 6: /* the clear command */
- break;
- }
- break;
-
- case CONFIGURE_ID: /* the Configure Menu */
- switch(itemNumber){
- case 1: /* Set Time: Heirarchical */
- case 2: /* Average Over: Heirarchical */
- case 3: /* Set Width: Heirarchical */
- break;
- case 4: /* spacer */
- break;
- case 5: /* Set Time: Heirarchical */
- case 6: /* Average Over: Heirarchical */
- case 7: /* Set Width: Heirarchical */
- break;
- case 8: /* spacer */
- break;
- case 9: /* Set Time: Heirarchical */
- case 10: /* Average Over: Heirarchical */
- case 11: /* Set Width: Heirarchical */
- break;
- case 12: /* spacer */
- break;
- case 13: /* Set Time: Heirarchical */
- case 14: /* Average Over: Heirarchical */
- case 15: /* Set Width: Heirarchical */
- break;
- case 16: /* spacer */
- break;
- case 17: /* compress juggler RAM... */
- MFMaxMem(&longPointer);
- break;
- } /* end switch */
- break;
-
- case BASE_ID:
- case (BASE_ID + 3):
- case (BASE_ID + 6):
- case (BASE_ID + 9):
-
- configSetting = ((menuID - BASE_ID)/3);
- CheckItem(theConfiguration[configSetting].ceiling.menuHandle, (int) **theConfiguration[configSetting].ceiling.valueHandle, FALSE);
- **theConfiguration[configSetting].ceiling.valueHandle = (SignedByte) itemNumber;
- CheckItem(theConfiguration[configSetting].ceiling.menuHandle, (int) **theConfiguration[configSetting].ceiling.valueHandle, TRUE);
- theConfiguration[configSetting].ceiling.value = CeilingSwitch(itemNumber);
- break;
-
- case (BASE_ID + 1):
- case (BASE_ID + 4):
- case (BASE_ID + 7):
- case (BASE_ID + 10):
- configSetting = (menuID - BASE_ID - 1)/3;
- CheckItem(theConfiguration[configSetting].interval.menuHandle, (int) **theConfiguration[configSetting].interval.valueHandle, FALSE);
- **theConfiguration[configSetting].interval.valueHandle = (SignedByte) (itemNumber);
- CheckItem(theConfiguration[configSetting].interval.menuHandle, (int) **theConfiguration[configSetting].interval.valueHandle, TRUE);
- theConfiguration[configSetting].interval.value = IntervalSwitch(itemNumber);
- break;
-
- case (BASE_ID + 2):
- case (BASE_ID + 5):
- case (BASE_ID + 8):
- case (BASE_ID + 11):
- configSetting = (menuID - BASE_ID - 2)/3;
- CheckItem(theConfiguration[configSetting].width.menuHandle, (int) **theConfiguration[configSetting].width.valueHandle, FALSE);
- **theConfiguration[configSetting].width.valueHandle = (SignedByte) itemNumber;
- CheckItem(theConfiguration[configSetting].width.menuHandle, (int) **theConfiguration[configSetting].width.valueHandle, TRUE);
- theConfiguration[configSetting].width.value = WidthSwitch(itemNumber);
- break;
-
- }
- HiliteMenu(0);
- }
-
-
- /* doLoad */
-
-
- doLoad()
- {
- long currentTicks, deltaTicks, barHeight, barWidth, leftEdge, rightEdge,
- topEdge, bottomEdge;
- Rect loadBar, eraseBar;
- int i;
-
-
- /* sample the load */
- currentTicks = TickCount();
- deltaTicks = currentTicks - ticksGoneBy;
- ticksGoneBy = currentTicks;
-
- /* increment accumulators and draw load */
- for (i = 0; i < 4; i++) {
- tickSum[i] += deltaTicks;
- tickSumDivider[i]++;
- if (tickSum[i] > theConfiguration[i].interval.value) {
-
- leftEdge = 5 + (i * BAROFFSET);
- bottomEdge = 35; /* will become resizable */
- topEdge = 0;
- barWidth = theConfiguration[i].width.value;
-
- barHeight = (40 - ((tickSum[i] * 35)/(tickSumDivider[i] * theConfiguration[i].ceiling.value))); /* arranged to eliminate decimals */
- SetRect (&loadBar, leftEdge, barHeight, leftEdge + barWidth, bottomEdge); /* left, top, right, bottom */
- SetRect (&eraseBar, leftEdge, topEdge, leftEdge + barWidth, barHeight);
- EraseRect(&eraseBar);
- PaintRect(&loadBar);
-
- tickSum[i] = 0;
- tickSumDivider[i] = 0;
- } /* endif */
- } /* end for */
- }
-
-
- /* quitTime */
-
-
- quitTime()
- { /* time to write resources, etc. */
- int i;
- Point p;
-
- for (i = 0; i < 4; i++) {
- ChangedResource(theConfiguration[i].ceiling.valueHandle);
- ChangedResource(theConfiguration[i].interval.valueHandle);
- ChangedResource(theConfiguration[i].width.valueHandle);
- }
-
- SetPort(loadWindow);
-
- p.h = 0;
- p.v = 0;
-
- LocalToGlobal(&p);
-
- **windowPoshHandle = p.h;
- **windowPosvHandle = p.v;
-
- ChangedResource((Handle) windowPosvHandle);
- ChangedResource((Handle) windowPoshHandle);
-
- ExitToShell();
- }
-
-
-
- /* main */
-
-
- main()
- {
-
- /* variables */
-
- EventRecord theEvent;
- Point eventPt;
- char eventChar;
- WindowPtr whichWindow, eventWindow;
- short windowCode, stillInGoAway, controlCode, whichItem,
- scrapIndex;
-
- setUpWorld();
- setUpMenus();
-
-
- while (TRUE) {
- WaitNextEvent(everyEvent, &theEvent, (long) NIL, (RgnHandle) NIL);
- switch (theEvent.what){
- case SUSPEND_RESUME_EVT:
- if ( ! BitTst( (Ptr) theEvent.message, SUSPEND_RESUME_BIT)) /* suspend event */
- ;
- else (BitTst( (Ptr) theEvent.message, SUSPEND_RESUME_BIT)) /* resume event */
- ;
- if ( ! BitTst( (Ptr) theEvent.message, CLIPBOARD_BIT)) /* don't convert clipboard */
- ;
- else (BitTst( (Ptr) theEvent.message, CLIPBOARD_BIT)) /* convert clipboard */
- ;
- break;
- case mouseDown:
- eventPt = theEvent.where;
- windowCode = FindWindow(eventPt, &eventWindow);
- switch (windowCode) {
- case inSysWindow:
- SystemClick(&theEvent, eventWindow);
- break;
- case inContent: /* won't work on minor switch b/c of compatibility issues w/Excel */
- case inGrow:
- case inGoAway:
- if (eventWindow == loadWindow)
- DragWindow(eventWindow, eventPt, &dragBoundsRect);
- break;
- case inDrag:
- DragWindow(eventWindow, eventPt, &dragBoundsRect);
- break;
- case inDesk:
- break;
- case inMenuBar:
- doMenu(MenuSelect(theEvent.where));
- break;
- default:
- break;
- } /* end switch(windowCode) */
- break;
-
- case keyDown:
- case autoKey:
- eventChar = theEvent.message;
- if (theEvent.modifiers & cmdKey) {
- doMenu(MenuKey(eventChar));
- HiliteMenu(0);
- }
- break;
- case activateEvt:
- break;
- case updateEvt: /* absolutely necessary, else will not get nullEvents initially */
- BeginUpdate(theEvent.message);
- EndUpdate(theEvent.message);
- break;
- case nullEvent:
- doLoad();
- break;
- default: ;
- } /* end of case theEvent.what */
- } /* while */
- }